<?php
    namespace app\controllers;
    use app\common\BaseController;
    use app\common\UploadForm;
    use app\models\Images;
    use app\models\User;
    use yii\web\UploadedFile;

    class OtherController extends BaseController{
        public function actionLogin(){
            $post = \Yii::$app->request->post();
            $msg = '登陆成功';
            $token = '';
            $code = 0;
            if ($post['username']){
                $userInfo = User::find()->where(['username' => $post['username']])->one();
                if ($userInfo && $userInfo->password === md5($post['password'])){
                    $token = md5(time());
                    $userInfo->token = $token;
                    if (!$userInfo->save()){
                        $msg = '登录失败';
                        $code = 1;
                    }
                }else{
                    $msg = '账号或者密码错误';
                    $code = 1;
                }
            }else{
                $msg = '登录失败';
                $code = 1;
            }
            return $this->result(['token' => $token], $code, $msg);
        }

        public function actionCheckNum(){
            $pass = \Yii::$app->request->get('pass');
            $enPass = md5(md5(md5(md5(md5(md5($pass))))));
            $rightPass = '92febdd6ce51d9deab73b00e03fae9f7';
            $code = 1;
            if ($enPass === $rightPass){
                $code = 0;
            }
            return $this->result([], $code);
        }

        private function uploadImage(){
            $model = new UploadForm();

            $path = '';
            if (\Yii::$app->request->isPost) {
                $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
                if (!$path = $model->upload()) {
                    // 文件上传成功
                    $path = '';
                }
            }
            return $path;
        }

        public function actionUpload(){
            $path = $this->uploadImage();
            $res = 0;
            $msg = '上传成功';
            if (!$path){
                $res = 1;
                $msg = '上传失败';
            }
            return $this->result($path, $res, $msg);
        }

        public function actionAddImage(){
            $path = $this->uploadImage();
            $res = 1;
            $result = [
                'path' => $path,
                'id' => 0
            ];
            if (!$path){
                $res = 1;
                $msg = '上传失败';
            }else{
                $postData = \Yii::$app->request->post();
                $postData['path'] = $path;
                $imageObj = new Images();
                if ($imageObj->load($postData, '') && $imageObj->save()){
                    $msg = '添加成功';
                    $res = 0;
                    $result['id'] = $imageObj->id;
                }else{
                    $msg = $imageObj->errors;
                }
            }
            return $this->result($result, $res, $msg);
        }
    }